import inquirer from 'inquirer'
import { exec, execSync } from 'child_process'

;(async function () {
  const store = execSync('git config --get credential.helper')
  if (store.toString() != 'store') {
    execSync('git config credential.helper store')
  }
  const argv = process.argv[2]
  if (argv == 'pull') {
    exec('npm i && git pull')
      .stdout?.on('data', (data) => {
        console.log(`stdout: ${data}`)
      })
      .on('error', (err) => {
        return console.error(err)
      })
      .on('end', () => {
        console.log('拉取完毕')
      })
  } else if (argv == 'push') {
    console.log("这里还得增加判断gpt仓库是否干净");
    
    const { commit } = await inquirer.prompt({
      name: 'commit',
      type: 'input',
      message: '请输入提交信息'
    })
    exec(`git add . && git commit -m "${commit}" && git push`)
      .stdout?.on('data', (data) => {
        console.log(`stdout: ${data}`)
      })
      .on('error', (err) => {
        return console.error(err)
      })
      .on('end', () => {
        console.log('提交成功')
      })
  }
})()
